const service=require('../../module/service.js');
const get_from = require('../../module/service.js').get_from;
const post_to = require('../../module/service.js').post_to;
Page({

  data: {
    shop_id: 0,
    banner_urls: [],
    rcmd_list: []
  },

  onShow(){
    
  },

  //页面加载时
  onLoad: function(options){  //可以根据全局变量curr_shop_id来指定加载的商店页面
    let shop_id = parseInt(options.shop_id);
    console.log('shop_id is: ', shop_id);
    //如果options中有对应的数据，便为data赋值
    if (shop_id)
    {
        getApp().globalData.curr_shop_id = shop_id;
        this.data.shop_id = shop_id;
    }

    wx.showLoading({
      title: '加载中...',
      mask:true
    });

    get_from('food/index', {shop_id: this.data.shop_id}).then((res)=>{
      if (res.data[0].imgUrls.length == 0)
      {
        let rand = Math.random() * (100 - 50) + 50;
        res.data[0].imgUrls.push({
          id: rand,
          src: "../../images/banner_1.png"
        });
      }
      this.setData({
        indexmessage:res.data
      });
      console.log(res.data)
      // setTimeout(() => {
      //   wx.hideLoading()
      // }, 500);
    },()=>{
      console.log("失败");
      wx.hideLoading();
    });

    get_from('banners/get_banner_urls', {shop_id: this.data.shop_id}).then((res)=>{
      // console.log(this.data.shop_id);
      // console.log(res.data);
      //设置banner_urls
      this.setData({
        banner_urls: res.data
      });
      setTimeout(() => {
        wx.hideLoading()
      }, 500);
    }, ()=>{
      console.log("失败");
      wx.hideLoading();
    });

    let user_id = getApp().globalData.userInfo.user_id;
    console.log(user_id)
    let rcmd_list = []
    // 获取推荐列表
    post_to('grpc/getRecmdListByUser', {shop_id: this.data.shop_id, user_id: user_id}).then((res)=>{
      if (res.data.msg === 'success')
      {
        // 作为推荐商品给用户
        rcmd_list = res.data.data
      }

      this.setData({
        rcmd_list: rcmd_list
      });
      console.log('推荐商品：',rcmd_list)
    });
  },

  //页面返回时
  onUnload(){
    //清除全局数据中的shopInfo
    let shopInfo = {
      curr_translist: [],
      curr_isgoods: 0,
      curr_count: 0,
      curr_iscpn: false
    }
    getApp().globalData.shopInfo = shopInfo;
  },

  onOrder:function(){
    wx.navigateTo({
      url: '../../pages/menu/menu',
    });
  },

  to_food_detail(e){
    let food_id = e.currentTarget.dataset.foodid
    wx.navigateTo({
      url: '/pages/fdetail/fdetail?food_id='+food_id+'&translist_id=-1'
    });
  },

  toKichen(e){
    let shop_id = this.data.shop_id;

    // 跳转到kitchen页面
    wx.navigateTo({
      url: '/pages/kitchen/kitchen?shop_id='+shop_id
    });
  }
})